<HTML>
 
<!-- Mirrored from www.javapractices.com/apps/movies/javadoc/src-html/hirondelle/movies/edit/MovieTableModel.html by HTTrack Website Copier/3.x [XR&CO'2010], Sun, 12 Jun 2011 17:30:20 GMT -->
<HEAD>
  <TITLE>
MovieTableModel.java
  </TITLE>
  <LINK REL ='stylesheet' TYPE='text/css' HREF='../../../../highlight.css' TITLE='Style'>
 </HEAD>
 <BODY>
<PRE>
<span class='keyword'>package</span> hirondelle.movies.edit;<a name=line.1></a>
<a name=line.2></a>
<span class='keyword'>import</span> java.util.Collections;<a name=line.3></a>
<span class='keyword'>import</span> java.util.Comparator;<a name=line.4></a>
<span class='keyword'>import</span> java.util.List;<a name=line.5></a>
<span class='keyword'>import</span> hirondelle.movies.util.Util;<a name=line.6></a>
<span class='keyword'>import</span> javax.swing.table.AbstractTableModel;<a name=line.7></a>
<a name=line.8></a>
<span class='comment'>/** <a name=line.9></a>
  Table model used by {@link javax.swing.JTable}, explicitly for {@link Movie} objects.  <a name=line.10></a>
 <a name=line.11></a>
  &lt;P&gt;When a database operation occurs, the view is refreshed by calling <a name=line.12></a>
  {@link #refreshView()}.<a name=line.13></a>
  &lt;P&gt;Note this class includes some methods which are unrelated to the needs of the <a name=line.14></a>
  superclass, but are useful in the context of this application. <a name=line.15></a>
 */</span><a name=line.16></a>
<span class='keyword'>public</span> <span class='keyword'>final</span> <span class='keyword'>class</span> MovieTableModel <span class='keyword'>extends</span> AbstractTableModel {<a name=line.17></a>
  <a name=line.18></a>
  <span class='comment'>/** Constructor. */</span><a name=line.19></a>
  <span class='keyword'>public</span> MovieTableModel(){<a name=line.20></a>
    fDAO = <span class='keyword'>new</span> MovieDAO();<a name=line.21></a>
    fMovies = fDAO.list();<a name=line.22></a>
  }<a name=line.23></a>
<a name=line.24></a>
  <span class='comment'>/** <a name=line.25></a>
   Explicitly refresh the view.<a name=line.26></a>
  <a name=line.27></a>
   &lt;P&gt;This style seems to be cleaner and simpler than implementing a listener on the DAO.<a name=line.28></a>
  */</span> <a name=line.29></a>
  <span class='keyword'>public</span> <span class='keyword'>void</span> refreshView() {<a name=line.30></a>
    fMovies = fDAO.list();<a name=line.31></a>
    <span class='comment'>//one might want to preserve the sort order here<a name=line.32></a>
</span>    fireTableDataChanged();<a name=line.33></a>
  }<a name=line.34></a>
  <a name=line.35></a>
  <span class='comment'>/** Returned the selected movie's id. */</span><a name=line.36></a>
  <span class='keyword'>public</span> String getId(<span class='keyword'>int</span> aRow){<a name=line.37></a>
    Movie movie = fMovies.get(aRow);<a name=line.38></a>
    <span class='keyword'>return</span> movie.getId(); <a name=line.39></a>
  }<a name=line.40></a>
  <a name=line.41></a>
  <span class='comment'>/** Return the selected {@link Movie}. */</span><a name=line.42></a>
  <span class='keyword'>public</span> Movie getMovie(<span class='keyword'>int</span> aRow){<a name=line.43></a>
    <span class='keyword'>return</span> fMovies.get(aRow);<a name=line.44></a>
  }<a name=line.45></a>
  <a name=line.46></a>
  <span class='comment'>/** <a name=line.47></a>
   Sort the movies.<a name=line.48></a>
   <a name=line.49></a>
   When called repeatedly, this method will toggle the sort between ascending and <a name=line.50></a>
   descending.<a name=line.51></a>
   @param aIdx index of the column by which to sort.  <a name=line.52></a>
  */</span><a name=line.53></a>
  <span class='keyword'>public</span> <span class='keyword'>void</span> sortByColumn(<span class='keyword'>int</span> aIdx){<a name=line.54></a>
    fNumClicks++;<a name=line.55></a>
    <span class='keyword'>if</span>( aIdx == <span class='literal'>1</span> ) {<a name=line.56></a>
      <span class='comment'>//natural sorting of the Movie class<a name=line.57></a>
</span>      Collections.sort(fMovies);<a name=line.58></a>
    }<a name=line.59></a>
    <span class='keyword'>else</span> {<a name=line.60></a>
      Comparator&lt;Movie&gt; comparator = <span class='keyword'>null</span>;<a name=line.61></a>
      <span class='keyword'>if</span> ( aIdx == <span class='literal'>0</span> ){<a name=line.62></a>
        comparator = Movie.TITLE_SORT;<a name=line.63></a>
      }<a name=line.64></a>
      <span class='keyword'>else</span> <span class='keyword'>if</span> ( aIdx == <span class='literal'>2</span> ){<a name=line.65></a>
        comparator = Movie.RATING_SORT;<a name=line.66></a>
      }<a name=line.67></a>
      <span class='keyword'>else</span> <span class='keyword'>if</span> ( aIdx == <span class='literal'>3</span> ){<a name=line.68></a>
        comparator = Movie.COMMENT_SORT;<a name=line.69></a>
      }<a name=line.70></a>
      Collections.sort(fMovies, comparator);<a name=line.71></a>
    }<a name=line.72></a>
    <span class='keyword'>if</span>( (fNumClicks % <span class='literal'>2</span>) == <span class='literal'>0</span>){<a name=line.73></a>
      Collections.reverse(fMovies);<a name=line.74></a>
    }<a name=line.75></a>
    fireTableDataChanged();<a name=line.76></a>
  }<a name=line.77></a>
  <a name=line.78></a>
  <span class='comment'>/** Return the number of columns in the table. */</span><a name=line.79></a>
  <span class='keyword'>public</span> <span class='keyword'>int</span> getColumnCount() {<a name=line.80></a>
    <span class='keyword'>return</span> <span class='literal'>4</span>;<a name=line.81></a>
  }<a name=line.82></a>
  <a name=line.83></a>
  <span class='comment'>/** Return the number of rows in the table. */</span><a name=line.84></a>
  <span class='keyword'>public</span> <span class='keyword'>int</span> getRowCount() {<a name=line.85></a>
    <span class='keyword'>return</span> fMovies.size();<a name=line.86></a>
  }<a name=line.87></a>
  <a name=line.88></a>
  <span class='comment'>/** Return the &lt;tt&gt;Object&lt;/tt&gt; in a specific table cell. */</span><a name=line.89></a>
  <span class='keyword'>public</span> Object getValueAt(<span class='keyword'>int</span> aRow, <span class='keyword'>int</span> aCol) {<a name=line.90></a>
    Object result = <span class='keyword'>null</span>;<a name=line.91></a>
    Movie movie = fMovies.get(aRow);<a name=line.92></a>
    <span class='keyword'>if</span>(aCol == <span class='literal'>0</span>) {<a name=line.93></a>
      result = movie.getTitle();<a name=line.94></a>
    }<a name=line.95></a>
    <span class='keyword'>else</span> <span class='keyword'>if</span>(aCol == <span class='literal'>1</span>) {<a name=line.96></a>
      result = Util.format(movie.getDateViewed());<a name=line.97></a>
    }<a name=line.98></a>
    <span class='keyword'>else</span> <span class='keyword'>if</span>(aCol == <span class='literal'>2</span>) {<a name=line.99></a>
      result = movie.getRating();<a name=line.100></a>
    }<a name=line.101></a>
    <span class='keyword'>else</span> <span class='keyword'>if</span>(aCol == <span class='literal'>3</span>) {<a name=line.102></a>
      result = movie.getComment();<a name=line.103></a>
    }<a name=line.104></a>
    <span class='keyword'>return</span> result;<a name=line.105></a>
  }<a name=line.106></a>
  <a name=line.107></a>
  <span class='comment'>/** Return the name of a specific column. */</span><a name=line.108></a>
  <span class='keyword'>public</span> String getColumnName(<span class='keyword'>int</span> aIdx){<a name=line.109></a>
    String result = <span class='literal'>""</span>;<a name=line.110></a>
    <span class='keyword'>if</span>( aIdx == <span class='literal'>0</span>) {<a name=line.111></a>
      result = <span class='literal'>"Title"</span>;<a name=line.112></a>
    }<a name=line.113></a>
    <span class='keyword'>else</span> <span class='keyword'>if</span>( aIdx == <span class='literal'>1</span>) {<a name=line.114></a>
      result = <span class='literal'>"Viewed"</span>;<a name=line.115></a>
    }<a name=line.116></a>
    <span class='keyword'>else</span> <span class='keyword'>if</span>( aIdx == <span class='literal'>2</span>) {<a name=line.117></a>
      result = <span class='literal'>"Rating"</span>;<a name=line.118></a>
    }<a name=line.119></a>
    <span class='keyword'>else</span> <span class='keyword'>if</span>( aIdx == <span class='literal'>3</span>) {<a name=line.120></a>
      result =  <span class='literal'>"Comment"</span>;<a name=line.121></a>
    }<a name=line.122></a>
    <span class='keyword'>return</span> result;<a name=line.123></a>
  }<a name=line.124></a>
  <a name=line.125></a>
  <span class='comment'>// PRIVATE //<a name=line.126></a>
</span>  <span class='keyword'>private</span> MovieDAO fDAO;<a name=line.127></a>
  <span class='keyword'>private</span> List&lt;Movie&gt; fMovies;<a name=line.128></a>
  <span class='keyword'>private</span> <span class='keyword'>int</span> fNumClicks = <span class='literal'>0</span>;<a name=line.129></a>
}<a name=line.130></a>
 <a name=line.131></a>
</PRE><a name=line.132></a>
 </BODY><a name=line.133></a>
<HTML><a name=line.134></a>
